/**
 * Created by LC on 2019/11/19
 */
package com.hw.paltfrom.service.mainscreen;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwDataEnterpriseHistoryMapper;
import com.hw.paltfrom.mapper.HwDataOverrunstationHistoryMapper;
import com.hw.paltfrom.pojo.HwDataEnterpriseHistory;
import com.hw.paltfrom.pojo.HwDataOverrunstationHistory;
import com.hw.paltfrom.pojo.RankListEntity;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.RankOverrunVO;
import com.hw.paltfrom.vo.SourceCarsRankListEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RankListService {
    @Autowired
    HwDataOverrunstationHistoryMapper hwDataOverrunstationHistoryMapper;//超限战历史数据

    @Autowired
    HwDataEnterpriseHistoryMapper hwDataEnterpriseHistoryMapper;//源头企业历史数据

    /**
     * 查询排行榜
     */
    public Map<String,Object> queryRankList(String dataType){
        //获取当天0点数据为endTime
        Calendar cal=Calendar.getInstance();
        //当天
        cal.add(Calendar.DATE,0);
        Date s1=cal.getTime();
        String endTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(s1);
        //获取startTime
        cal.add(Calendar.DATE,-100);
        Date s2=cal.getTime();
        String startTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(s2);
        /*Date s2 = new Date(System.currentTimeMillis()-1000*60*60*24*365L);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sdf.format(s2);*/

        List<RankListEntity> rankListEntities = new LinkedList<>();
        List<SourceCarsRankListEntity> sourceCarsRankListEntities = new LinkedList<>();
        Integer total = 0;
        if ("检测车辆".equals(dataType)){
            rankListEntities = hwDataOverrunstationHistoryMapper.dtRankList(startTime,endTime);
            //查询动态数据总数
            Wrapper<HwDataOverrunstationHistory> overrunstationHistoryWrapper = new EntityWrapper<>();
            overrunstationHistoryWrapper.between("D_JCSJ",new Date(System.currentTimeMillis()-1000*60*60*24L),new Date());
            total = hwDataOverrunstationHistoryMapper.selectCount(overrunstationHistoryWrapper);
        }else if ("超限车辆".equals(dataType)){
            rankListEntities = hwDataOverrunstationHistoryMapper.jtRankList(startTime, endTime);
            //查询静态数据总数
            Wrapper<HwDataOverrunstationHistory> overrunstationHistoryWrapper = new EntityWrapper<>();
            overrunstationHistoryWrapper.eq("D_SFCX",1).between("J_JCSJ",new Date(System.currentTimeMillis()-1000*60*60*24L),new Date());
            total = hwDataOverrunstationHistoryMapper.selectCount(overrunstationHistoryWrapper);
        }else if ("源头企业".equals(dataType)){
            sourceCarsRankListEntities = hwDataEnterpriseHistoryMapper.ytccclRankList(startTime, endTime);
            Wrapper<HwDataEnterpriseHistory> enterpriseHistoryMapperWrapper = new EntityWrapper<>();
            enterpriseHistoryMapperWrapper.between("CCSJ",new Date(System.currentTimeMillis()-1000*60*60*24L),new Date());
            total = hwDataEnterpriseHistoryMapper.selectCount(enterpriseHistoryMapperWrapper);
        }else {
            return ResponeResultUtil.fail(HWSystemCode.DATA_NOTFOUND);
        }

        //创建返回排行榜结果
        RankOverrunVO rankOverrunVO = new RankOverrunVO();
        rankOverrunVO.setRankListEntities(rankListEntities);
        rankOverrunVO.setSourceCarsRankListEntities(sourceCarsRankListEntities);
        rankOverrunVO.setTotal(total);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,rankOverrunVO);
    }


}
